<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/10/9
 * Time: 16:10
 */
namespace app\home\controller;
use app\common\controller\HomeBase;

class Search extends HomeBase{
    /**
     * 搜索找人
     */
    public function sech_user(){
        //搜索找人 get传输过来的数据
        $keyword = $this->_getKeyword();

        if ($keyword) {
            //检索出除自己之外称含关键字的用户
            $db=db('userinfo');
            //组合查询条件
            $where = [
                'username' => ['like','%' . $keyword . '%'],
                'uid' => ['neq',session('uid')]
            ];
            //要查询的字段
            $field = ['id','username','sex','location','intro','face80','follow','fans','weibo','uid'];
            $objData = $db->where($where)->field($field)->order('fans')->paginate(5);
            $result = [];
            //循环对象
            foreach ($objData as $data) {
                $result[] = [
                    'username' => $data['username'],
                    'sex' => $data['sex'],
                    'location' => $data['location'],
                    'intro' => $data['intro'],
                    'face80' => $data['face80'],
                    'follow' => $data['follow'],
                    'fans' => $data['fans'],
                    'weibo' => $data['weibo'],
                    'uid' => $data['uid'],
                    'id' => $data['id'],
                ];
            }
            //重新组合结果集，得到是否已关注与是否互相关注
            $result=$this->_getMutual($result);

            //判断  搜索之后返回的结果集是否为空
            $this->assign([
                'result' => $result ? $result : false,
                'page' => $objData->render()
            ]);
        } else {
            $this->assign([
                'result' => []
            ]);
        }
        $this->assign([
            'keyword' => $keyword, // 分配搜索框的值
        ]);
        return $this->fetch('sech_user');

    }
    /**
     * 搜索微博
     */
    public function sech_weibo () {
        //搜索微博 get传输过来的数据
        $keyword = $this->_getKeyword();
        if ($keyword) {
            //检索含有关键字的微博
            $where = ['w.content' => ['LIKE', '%' . $keyword . '%']];
            $weibo = new \app\common\model\Weibo();
            $result = $weibo->getListData($where);
            $this->assign([
                'data' => $result
            ]);
        } else {
            $this->assign([
                'data' => [
                    'page' => '',
                    'data' => []
                ]
            ]);
        }
        return $this->fetch('sech_weibo',['keyword' => $keyword]);
    }

    /*
    ** 返回搜索关键字
    */
    private function _getKeyword(){
        return input('get.keyword') =='搜索微博、找人' ? NULL : input('get.keyword');
    }

    /*
    ** 重新组合结果集得到是否互相关注与是否已关注
     * @param  [Array] $result [需要处理的结果集]
     * @return [Array]         [处理完成后的结果集]
     */
    private function _getMutual($result){
        //如果没有结果，直接返回假
        if (!$result) return false;

        $db = db('follow');
        //遍历结果集
        foreach ($result as $k => $v) {
            //联合查询出是否互相关注 union相当于PHP中的合并数组
            $sql = 	'(SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . $v['uid'] . ' AND `fans` = ' . session('uid'). ') UNION (SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . session('uid'). ' AND `fans` = ' . $v['uid'] . ')';
            $mutual=$db->query($sql);

            //统计查询出是否已互相关注的条数 如果等于二 那么就代表已互相关注
            if (count($mutual) == 2) {
                //互相关注状态 就代表我也是他的粉丝
                $result[$k]['mutual'] = 1; //代表互相关注状态
                $result[$k]['followed'] = 1; //已关注对方
            } else {
                $result[$k]['mutual'] = 0; //代表不是互相关注状态

                //未关注时检索出是否  已关注
                $where = [
                    'follow' => $v['uid'],		//关注人ID
                    'fans'   => session('uid')	//粉丝ID
                ];
                $result[$k]['followed']=$db->where($where)->count();//统计已关注对方数目
            }
        }
        return $result;
    }
}